package cn.com.ddapp.ddm.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.MyDraftAdapter;
import cn.com.ddapp.ddm.adapter.ShareListAdapter;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.CircleBean;
import cn.com.ddapp.ddm.inter.OkUtilCallback;
import cn.com.ddapp.ddm.ui.empty.EmptyLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.PullToRefreshLayout;
import cn.com.ddapp.ddm.widget.pulltorefresh.pullableview.PullableListView;
import okhttp3.Call;


/**
 * Created by Administrator on 2016/7/29 0029.
 * 我的草稿箱
 */
public class MyDraftActivity extends BaseActivity {
	@BindView(R.id.share_refresh)
	PullToRefreshLayout share_refresh;
	@BindView(R.id.share_list)
	PullableListView share_list;
	@BindView(R.id.empty)
	EmptyLayout emptyLayout;
	@BindView(R.id.myfound_title)
	TextView myfound_title;
	@BindView(R.id.nearshop)
	TextView nearshop;
	@BindView(R.id.right_share)
	ImageView right_share;
	@BindView(R.id.found_back)
	ImageView found_back;
	private ShareListAdapter shareListAdapter;
	private MyDraftAdapter draftAdapter;
	private List<CircleBean.CircleList> stringList;
	private List<CircleBean.CircleList> draftList;
	private int st;
	private String type,userId;

	public static void startIntent(Context context){
		Intent intent=new Intent(context,MyDraftActivity.class);
		context.startActivity(intent);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		type=getIntent().getStringExtra("type");
		userId=getIntent().getStringExtra("userId");
		if(type.equals("1")){
			myfound_title.setText("我的分享");
			right_share.setVisibility(View.VISIBLE);
			nearshop.setVisibility(View.VISIBLE);
			found_back.setVisibility(View.GONE);
		}if(type.equals("0")){
			myfound_title.setText("我的草稿箱");
			right_share.setVisibility(View.GONE);
			nearshop.setVisibility(View.GONE);
			found_back.setVisibility(View.VISIBLE);
		}
		requestData(0,type);
		share_refresh.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
				share_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
				st=0;
                requestData(st,type);
			}

			@Override
			public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
				st=st+10;
				requestLoadData(st,type);
			}
		});
	}

	@Override
	public void initView() {

	}

	@Override
	public void initData() {

	}

	@Override
	public int getContentViewId() {
		return R.layout.found_fragment;
	}

	@Override
	protected String getFragmentTag() {
		return null;
	}


	private void requestData(int st,final String tmtype) {
		if(type.equals("1")){
			emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
			final OkUtilCallback okUtilCallback=new OkUtilCallback(emptyLayout) {
				@Override
				public void onMyResponse(String response, int id) {
					CircleBean bean=gson.fromJson(response,CircleBean.class);
					if(!bean.getResult().equals("OK")){
						emptyLayout.setErrorType(EmptyLayout.NODATA);
						BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
					}else if(bean.getResult().equals("OK") && bean.getCircleList().size()==0){
						emptyLayout.setErrorType(EmptyLayout.NODATA);
					}else{
						stringList=bean.getCircleList();
						shareListAdapter=new ShareListAdapter(MyDraftActivity.this,stringList);
						share_list.setAdapter(shareListAdapter);

					}
				}

                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
            };
			String params="userId=" +userId+"&st="+st;
			OKUtilAPI.post(OKUtilAPI.circlelist, params, okUtilCallback);
		}else{
			emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
			final OkUtilCallback okUtilCallback=new OkUtilCallback(emptyLayout) {
				@Override
				public void onMyResponse(String response, int id) {
					CircleBean bean=gson.fromJson(response,CircleBean.class);
					if(!bean.getResult().equals("OK")){
						emptyLayout.setErrorType(EmptyLayout.NODATA);
						BaseApplication.getIntstance().showToast(bean.getResult(), Toast.LENGTH_SHORT,0, Gravity.CENTER);
					}else if(bean.getResult().equals("OK") && bean.getCircleList().size()==0){
						emptyLayout.setErrorType(EmptyLayout.NODATA);
					}else{
						draftList=bean.getCircleList();
						draftAdapter=new MyDraftAdapter(MyDraftActivity.this,draftList);
						share_list.setAdapter(draftAdapter);

					}
				}

                @Override
                public void onError(Call call, Exception e, int id) {
                    super.onError(call, e, id);
                    emptyLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                }
            };
			String params="userId=" +userId+"&status="+type+"&st="+st;
			OKUtilAPI.post(OKUtilAPI.circlelist, params, okUtilCallback);
		}


	}
	private void requestLoadData(final int a,final String retype) {
//		emptyLayout.setErrorType(EmptyLayout.NETWORK_LOADING);
//		final OkUtilCallback okUtilCallback =new OkUtilCallback() {
//			@Override
//			public void onMyResponse(String response, int id) {
//				NearStoreBean bean = gson.fromJson(response, NearStoreBean.class);
//				List<NearStoreBean.ShopBean> ll=bean.getShopDisList();
//				Log.i("yu","--size-"+ll.size());
//				if(ll.size()>0){
//					st=a;
//					nAdapter.addView(ll);
//					nAdapter.notifyDataSetChanged();
//					nearshop_refresh.refreshFinish(PullToRefreshLayout.SUCCEED);
//				}else{
//				}
//			}
//		};
//
//		String params="longitude=" +"150"+"&latitude="+"200"+"&st="+st;
//		OKUtilAPI.post(OKUtilAPI.nearstore, params, okUtilCallback);
		share_refresh.loadmoreFinish(PullToRefreshLayout.NODATA);
	}


	private void reequestData() {

	}


	@OnClick({R.id.img_error_layout,R.id.right_share,R.id.nearshop,R.id.found_back})
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.img_error_layout:
				requestData(0,type);
				break;
			case R.id.right_share:
				ToShareActivity.startIntent(MyDraftActivity.this);
				break;
			case R.id.nearshop:
				NearStoreActivity.startIntent(MyDraftActivity.this);
				break;
			case R.id.found_back:
				finish();
			break;

		}
	}

}
